import { defineStore } from 'pinia'
import type { Product } from '../types'

interface ProductState {
  products: Product[]
  loading: boolean
  error: string | null
}

// 模拟商品数据
const mockProducts: Product[] = [
  {
    id: 1,
    title: "2024新款笔记本电脑",
    price: 4999.00,
    description: "15.6英寸大屏幕，16GB内存，512GB固态硬盘，高性能处理器，轻薄便携",
    image: "https://picsum.photos/id/1/400/400"
  },
  {
    id: 2,
    title: "无线蓝牙耳机",
    price: 299.00,
    description: "主动降噪，40小时续航，高清音质，舒适佩戴",
    image: "https://picsum.photos/id/2/400/400"
  },
  {
    id: 3,
    title: "智能手表",
    price: 899.00,
    description: "心率监测，运动追踪，防水设计，来电提醒，长续航",
    image: "https://picsum.photos/id/3/400/400"
  },
  {
    id: 4,
    title: "4K高清摄像机",
    price: 3299.00,
    description: "专业级影像质量，防抖技术，广角镜头，智能对焦",
    image: "https://picsum.photos/id/4/400/400"
  },
  {
    id: 5,
    title: "机械键盘",
    price: 399.00,
    description: "青轴机械轴体，RGB背光，人体工学设计，即插即用",
    image: "https://picsum.photos/id/5/400/400"
  },
  {
    id: 6,
    title: "游戏手柄",
    price: 199.00,
    description: "人体工学设计，双振动反馈，即插即用，兼容多平台",
    image: "https://picsum.photos/id/6/400/400"
  },
  {
    id: 7,
    title: "无线充电器",
    price: 129.00,
    description: "15W快充，多设备兼容，智能温控，LED指示灯",
    image: "https://picsum.photos/id/7/400/400"
  },
  {
    id: 8,
    title: "便携式移动电源",
    price: 199.00,
    description: "20000mAh大容量，双向快充，LED显示，多重保护",
    image: "https://picsum.photos/id/8/400/400"
  }
]

export const useProductStore = defineStore('product', {
  state: (): ProductState => ({
    products: [],
    loading: false,
    error: null
  }),
  
  actions: {
    async fetchProducts(): Promise<void> {
      this.loading = true
      try {
        await new Promise(resolve => setTimeout(resolve, 500))
        this.products = mockProducts
      } catch (error) {
        this.error = error instanceof Error ? error.message : '未知错误'
      } finally {
        this.loading = false
      }
    },
    
    getProductById(id: number): Product | undefined {
      return this.products.find(product => product.id === id)
    }
  }
}) 